home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Demos / Codeworks 0.94b3 / Codeworks® WWW Demo Doc. / Scripting Manual.doc / Scripting Manual.doc.rsrc / TEXT_143.txt < prev    next >
Encoding:
Text File  |  1995-05-01  |  1.8 KB  |  29 lines

  1. Writing Scripts - Self
  2.  
  3.     Now that cash-register can perform a number of useful actions, you can tie them all together.  Add several buttons to the cash-register form view as follows:
  4.  
  5.     Add a button called buy-milk with the script:
  6.         ring-up-item self price 1.59
  7.  
  8.     Add a button called buy-eggs with the script:
  9.         ring-up-item self price 0.205 quantity 12
  10.  
  11.     Add a button called buy-cheese with the script:
  12.         ring-up-item self price 5.45 quantity 0.5
  13.             buying half a pound of cheese at 5.45/pound
  14.  
  15.     Add a button called grand-total with the script:
  16.         $ amt-off, tax.
  17.         amt-off := self discount on sub-total.
  18.         tax := (sub-total - amt-off) * 0.0725.
  19.         total := sub-total - amt-off + tax.
  20.         daily-total := daily-total + total.
  21.         sub-total := 0.
  22.  
  23.  
  24.     Notice than in all these scripts, the messages were sent to something called self, not cash-register.  Self is a predeclared variable that has the value of the receiver of the message.  Since tapping on these buttons causes the a message to be sent with cash-register as the receiver, self has the value of cash-register.
  25.  
  26.     If you replaced every occurrence of self in these scripts with cash-register, then they would work exactly the same.  Self is better because if you renamed cash-register, and the scripts named cash-register they would no longer work.  When the scripts use self, they operate on the correct object, no matter what you name it.  The same is true if you make instances of cash-register: if the scripts use cash-register, they always operate on the class.  If they use self, the work correctly. 
  27.  
  28.     You cannot use self when you send messages to the cash-register from the workspace.  This is because for scripts in the workspace, the value of self is the workspace, and the workspace doesn‚Äôt understand messages like ring-up-item or discount.
  29.